package com.imagination.applet.controller.api.callback;

import com.imagination.applet.controller.api.request.AiTaskCallbackParam;
import com.imagination.applet.domain.product.model.ProductImage;
import com.imagination.applet.domain.product.repository.ProductImageRepository;
import com.imagination.applet.domain.product.service.ProductImageQueryService;
import com.imagination.applet.domain.virtualidentity.service.UserVirtualIdentityCommandService;
import com.imagination.applet.infrastructure.common.RestResponse;
import com.imagination.applet.infrastructure.logging.Log;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/ai-task")
//@Tag(name = "AI任务回调服务")
@Slf4j
public class FaceChainCallbackController {

    @Resource
    private UserVirtualIdentityCommandService userVirtualIdentityCommandService;
    @Resource
    private ProductImageQueryService productImageQueryService;
    @Resource
    private ProductImageRepository productImageRepository;

    @Log
    @PostMapping(value = "/callback")
//    @Operation(summary = "AI任务回调（待实现）")
    public RestResponse<Void> training(@RequestBody AiTaskCallbackParam param) {
        Integer callbackType = param.getCallbackType();
        log.info("AI任务回调 param = {}", param);
        String taskId = param.getTaskId();
        if (callbackType == 1) {
            String virtualIdentityId = taskId.replace("virtual-identity-", "");
            if (param.getTrainStatus() == 1) {
                userVirtualIdentityCommandService.succeed(Long.parseLong(virtualIdentityId), new HashMap<>());
            } else {
                userVirtualIdentityCommandService.failed(Long.parseLong(virtualIdentityId));
            }
        } else if (callbackType == 2) {
            List<ProductImage> productImageList = productImageQueryService.getByTaskId(taskId);
            for (int i = 0; i < productImageList.size(); i++) {
                ProductImage productImage = productImageList.get(i);
                if (param.getTrainStatus() == 1) {
                    productImage.setTrainingStatus(1);
                    LocalDateTime now = LocalDateTime.now();
                    String date = now.format(DateTimeFormatter.ofPattern("yyyy_M_d"));
                    int index = i + 1;
                    productImage.setImagePath("facechain/inpaint_result/" + date + "/inpaint_" + taskId + "_" + index + ".png");
                }
                if(param.getTrainStatus()==-1){
                    productImage.setTrainingStatus(-1);
                    productImage.setDeleted(1);
                }
            }
            productImageRepository.saveAll(productImageList);
        }
        return RestResponse.ok();
    }

}
